import { Zodiac } from "./zodiac";

interface ZodiacType {
  [key: string]: number[];
}
interface ConfigType {
  color: {
    [key: string]: number[];
  };
  year: {
    shu: ZodiacType;
    niu: ZodiacType;
    hu: ZodiacType;
    tu: ZodiacType;
    long: ZodiacType;
    she: ZodiacType;
    ma: ZodiacType;
    yang: ZodiacType;
    hou: ZodiacType;
    ji: ZodiacType;
    gou: ZodiacType;
    zhu: ZodiacType;
  };
  five: Record<string, number[]>;
  animal: {
    poultry: {
      [key: string]: string;
    };
    beast: {
      [key: string]: string;
    };
  };
  sum: {
    joindan: number[];
    joinshuang: number[];
    joinda: number[];
    joinxiao: number[];
    dadan: number[];
    dashuang: number[];
    xiaodan: number[];
    xiaoshuang: number[];
  };
}

const config: ConfigType = {
  color: {
    red: [1, 2, 7, 8, 12, 13, 18, 19, 23, 24, 29, 30, 34, 35, 40, 45, 46],
    blue: [3, 4, 9, 10, 14, 15, 20, 25, 26, 31, 36, 37, 41, 42, 47, 48],
    green: [5, 6, 11, 16, 17, 21, 22, 27, 28, 32, 33, 38, 39, 43, 44, 49]
  },
  year: {
    shu: {
      shu: [1, 13, 25, 37, 49],
      niu: [12, 24, 36, 48],
      hu: [11, 23, 35, 47],
      tu: [10, 22, 34, 46],
      long: [9, 21, 33, 45],
      she: [8, 20, 32, 44],
      ma: [7, 19, 31, 43],
      yang: [6, 18, 30, 42],
      hou: [5, 17, 29, 41],
      ji: [4, 16, 28, 40],
      gou: [3, 15, 27, 39],
      zhu: [2, 14, 26, 38]
    },
    niu: {
      shu: [2, 14, 26, 38],
      niu: [1, 13, 25, 37, 49],
      hu: [12, 24, 36, 48],
      tu: [11, 23, 35, 47],
      long: [10, 22, 34, 46],
      she: [9, 21, 33, 45],
      ma: [8, 20, 32, 44],
      yang: [7, 19, 31, 43],
      hou: [6, 18, 30, 42],
      ji: [5, 17, 29, 41],
      gou: [4, 16, 28, 40],
      zhu: [3, 15, 27, 39]
    },
    hu: {
      shu: [3, 15, 27, 39],
      niu: [2, 14, 26, 38],
      hu: [1, 13, 25, 37, 49],
      tu: [12, 24, 36, 48],
      long: [11, 23, 35, 47],
      she: [10, 22, 34, 46],
      ma: [9, 21, 33, 45],
      yang: [8, 20, 32, 44],
      hou: [7, 19, 31, 43],
      ji: [6, 18, 30, 42],
      gou: [5, 17, 29, 41],
      zhu: [4, 16, 28, 40]
    },
    tu: {
      shu: [4, 16, 28, 40],
      niu: [3, 15, 27, 39],
      hu: [2, 14, 26, 38],
      tu: [1, 13, 25, 37, 49],
      long: [12, 24, 36, 48],
      she: [11, 23, 35, 47],
      ma: [10, 22, 34, 46],
      yang: [9, 21, 33, 45],
      hou: [8, 20, 32, 44],
      ji: [7, 19, 31, 43],
      gou: [6, 18, 30, 42],
      zhu: [5, 17, 29, 41]
    },
    long: {
      shu: [5, 17, 29, 41],
      niu: [4, 16, 28, 40],
      hu: [3, 15, 27, 39],
      tu: [2, 14, 26, 38],
      long: [1, 13, 25, 37, 49],
      she: [12, 24, 36, 48],
      ma: [11, 23, 35, 47],
      yang: [10, 22, 34, 46],
      hou: [9, 21, 33, 45],
      ji: [8, 20, 32, 44],
      gou: [7, 19, 31, 43],
      zhu: [6, 18, 30, 42]
    },
    she: {
      shu: [6, 18, 30, 42],
      niu: [5, 17, 29, 41],
      hu: [4, 16, 28, 40],
      tu: [3, 15, 27, 39],
      long: [2, 14, 26, 38],
      she: [1, 13, 25, 37, 49],
      ma: [12, 24, 36, 48],
      yang: [11, 23, 35, 47],
      hou: [10, 22, 34, 46],
      ji: [9, 21, 33, 45],
      gou: [8, 20, 32, 44],
      zhu: [7, 19, 31, 43]
    },
    ma: {
      shu: [7, 19, 31, 43],
      niu: [6, 18, 30, 42],
      hu: [5, 17, 29, 41],
      tu: [4, 16, 28, 40],
      long: [3, 15, 27, 39],
      she: [2, 14, 26, 38],
      ma: [1, 13, 25, 37, 49],
      yang: [12, 24, 36, 48],
      hou: [11, 23, 35, 47],
      ji: [10, 22, 34, 46],
      gou: [9, 21, 33, 45],
      zhu: [8, 20, 32, 44]
    },
    yang: {
      shu: [8, 20, 32, 44],
      niu: [7, 19, 31, 43],
      hu: [6, 18, 30, 42],
      tu: [5, 17, 29, 41],
      long: [4, 16, 28, 40],
      she: [3, 15, 27, 39],
      ma: [2, 14, 26, 38],
      yang: [1, 13, 25, 37, 49],
      hou: [12, 24, 36, 48],
      ji: [11, 23, 35, 47],
      gou: [10, 22, 34, 46],
      zhu: [9, 21, 33, 45]
    },
    hou: {
      shu: [9, 21, 33, 45],
      niu: [8, 20, 32, 44],
      hu: [7, 19, 31, 43],
      tu: [6, 18, 30, 42],
      long: [5, 17, 29, 41],
      she: [4, 16, 28, 40],
      ma: [3, 15, 27, 39],
      yang: [2, 14, 26, 38],
      hou: [1, 13, 25, 37, 49],
      ji: [12, 24, 36, 48],
      gou: [11, 23, 35, 47],
      zhu: [10, 22, 34, 46]
    },
    ji: {
      shu: [10, 22, 34, 46],
      niu: [9, 21, 33, 45],
      hu: [8, 20, 32, 44],
      tu: [7, 19, 31, 43],
      long: [6, 18, 30, 42],
      she: [5, 17, 29, 41],
      ma: [4, 16, 28, 40],
      yang: [3, 15, 27, 39],
      hou: [2, 14, 26, 38],
      ji: [1, 13, 25, 37, 49],
      gou: [12, 24, 36, 48],
      zhu: [11, 23, 35, 47]
    },
    gou: {
      shu: [11, 23, 35, 47],
      niu: [10, 22, 34, 46],
      hu: [9, 21, 33, 45],
      tu: [8, 20, 32, 44],
      long: [7, 19, 31, 43],
      she: [6, 18, 30, 42],
      ma: [5, 17, 29, 41],
      yang: [4, 16, 28, 40],
      hou: [3, 15, 27, 39],
      ji: [2, 14, 26, 38],
      gou: [1, 13, 25, 37, 49],
      zhu: [12, 24, 36, 48]
    },
    zhu: {
      shu: [12, 24, 36, 48],
      niu: [11, 23, 35, 47],
      hu: [10, 22, 34, 46],
      tu: [9, 21, 33, 45],
      long: [8, 20, 32, 44],
      she: [7, 19, 31, 43],
      ma: [6, 18, 30, 42],
      yang: [5, 17, 29, 41],
      hou: [4, 16, 28, 40],
      ji: [3, 15, 27, 39],
      gou: [2, 14, 26, 38],
      zhu: [1, 13, 25, 37, 49]
    }
  },
  five: getFive(),
  animal: {
    poultry: {
      niu: "牛",
      ma: "马",
      yang: "羊",
      ji: "鸡",
      gou: "狗",
      zhu: "猪"
    },
    beast: {
      shu: "鼠",
      hu: "虎",
      tu: "兔",
      long: "龙",
      she: "蛇",
      hou: "猴"
    }
  },
  sum: {
    // 合单
    joindan: [
      1, 3, 5, 7, 9, 10, 12, 14, 16, 18, 21, 23, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 45, 47, 49
    ],
    // 合双
    joinshuang: [
      2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, 28, 31, 33, 35, 37, 39, 40, 42, 44, 46, 48
    ],
    //合大
    joinda: [
      7, 8, 9, 16, 17, 18, 19, 25, 26, 27, 28, 29, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48,
      49
    ],
    //合小
    joinxiao: [
      1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24, 30, 31, 32, 33, 40, 41, 42
    ],
    // 大单
    dadan: [25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49],
    //大双
    dashuang: [26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48],
    //小单
    xiaodan: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23],
    //小双
    xiaoshuang: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
  }
};
// 获取甲子
function GetJiazhi() {
  const str =
    "甲子,乙丑,丙寅,丁卯,戊辰,己巳,庚午,辛未,壬申,癸酉,甲戌,乙亥,丙子,丁丑,戊寅,己卯,庚辰,辛巳,壬午,癸未,甲申,乙酉,丙戌,丁亥,戊子,己丑,庚寅,辛卯,壬辰,癸巳,甲午,乙未,丙申,丁酉,戊戌,己亥,庚子,辛丑,壬寅,癸卯,甲辰,乙巳,丙午,丁未,戊申,己酉,庚戌,辛亥,壬子,癸丑,甲寅,乙卯,丙辰,丁巳,戊午,己未,庚申,辛酉,壬戌,癸亥";
  return str.split(",");
}
// 获取甲子对应五行
function GetNayin(jiazhi: string) {
  const str =
    "甲子乙丑海中金,丙寅丁卯炉中火,戊辰己巳大林木,庚午辛未路旁土,壬申癸酉剑锋金,甲戌乙亥山头火,丙子丁丑涧下水,戊寅己卯城头土,庚辰辛巳白腊金,壬午癸未杨柳木,甲申乙酉泉中水,丙戌丁亥屋上土,戊子己丑劈雳火,庚寅辛卯松柏木,壬辰癸巳长流水,甲午乙未沙中金,丙申丁酉山下火,戊戌己亥平地木,庚子辛丑壁上土,壬寅癸卯金箔金,甲辰乙巳佛灯火,丙午丁未天河水,戊申己酉大驿土,庚戌辛亥插环金,壬子癸丑桑枝木,甲寅乙卯大溪水,丙辰丁巳沙中土,戊午己未天上火,庚申辛酉石榴木,壬戌癸亥大海水";
  const arr = str.split(",");
  for (const key in arr) {
    if (arr[key].includes(jiazhi)) {
      return arr[key].substr(-1);
    }
  }
}
// 获取当前年的五行
export function getFive(): Record<string, number[]> {
  const jiazhiList = GetJiazhi();
  const nayinList = jiazhiList.map(item => GetNayin(item));
  const five: Record<string, number[]> = {
    jin: [],
    mu: [],
    shui: [],
    huo: [],
    tu: []
  };
  const currentYear = new Date().getFullYear();
  for (let i = 1; i <= 49; i++) {
    const index = currentYear - 1922 - i - 1;
    const itemName = nayinList[index % 60];
    if (itemName == "金") {
      five.jin.push(i);
    }
    if (itemName == "木") {
      five.mu.push(i);
    }
    if (itemName == "水") {
      five.shui.push(i);
    }
    if (itemName == "火") {
      five.huo.push(i);
    }
    if (itemName == "土") {
      five.tu.push(i);
    }
  }
  return five;
}
// 返回当年生肖
export function getZodiac() {
  return new Zodiac().getZodiac();
}
// 获取当前生肖对应的号码
export function getZodiacJson() {
  const zodiac = getZodiac();
  switch (zodiac) {
    case "鼠":
      return config.year.shu;
    case "牛":
      return config.year.niu;
    case "虎":
      return config.year.hu;
    case "兔":
      return config.year.tu;
    case "龙":
      return config.year.long;
    case "蛇":
      return config.year.she;
    case "马":
      return config.year.ma;
    case "羊":
      return config.year.yang;
    case "猴":
      return config.year.hou;
    case "鸡":
      return config.year.ji;
    case "狗":
      return config.year.gou;
    case "猪":
      return config.year.zhu;
  }
  return config.year.shu;
}
// 获取家禽号码
export function getPoultry() {
  const keys = Object.keys(config.animal.poultry);
  const json = getZodiacJson();
  const nums: number[] = [];
  keys.forEach(key => {
    nums.push(...json[key]);
  });
  return nums.sort((a, b) => a - b);
}
// 获取家禽生肖
export function getPoultryName() {
  return Object.values(config.animal.poultry);
}
// 获取走兽号码
export function getBeast() {
  const keys = Object.keys(config.animal.beast);
  const json = getZodiacJson();
  const nums: number[] = [];
  keys.forEach(key => {
    nums.push(...json[key]);
  });
  return nums.sort((a, b) => a - b);
}
// 获取走兽生肖
export function getBeastName() {
  return Object.values(config.animal.beast);
}
// 获取波色号码
export function getColor(color = "red") {
  switch (color) {
    case "red":
      return config.color.red;
    case "blue":
      return config.color.blue;
    case "green":
      return config.color.green;
    default:
      return config.color.red;
  }
}
// 获取大小
export function getBigOrSmall(flag = true) {
  const nums = [];
  for (let i = 1; i < 50; i++) {
    nums.push(i);
  }
  if (flag) {
    return nums.filter(num => num > 24);
  } else {
    return nums.filter(num => num <= 24);
  }
}
// 获取单双
export function getSingleOrDouble(flag = true) {
  const nums = [];
  for (let i = 1; i < 50; i++) {
    nums.push(i);
  }
  if (flag) {
    return nums.filter(num => num % 2 != 0);
  } else {
    return nums.filter(num => num % 2 == 0);
  }
}
// 获取合单双
export function getSumSingleOrDouble(flag = true) {
  if (flag) {
    return config.sum.joindan;
  } else {
    return config.sum.joinshuang;
  }
}
// 获取合大小
export function getSumBigOrSmall(flag = true) {
  if (flag) {
    return config.sum.joinda;
  } else {
    return config.sum.joinxiao;
  }
}
// 格式化六合彩号码排序
export function formatNumberSorting(arr: any[]) {
  const indexs = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
  const newList: any[] = [];
  indexs.forEach(i => {
    arr.forEach((item: any) => {
      const arr = item.nums.split("");
      if (arr[arr.length - 1] == i) {
        newList.push(item);
      }
    });
  });
  return newList;
}
// 获取号码颜色
export function getBallColor(num: number) {
  if (config.color.red.includes(num)) {
    return "red";
  } else if (config.color.blue.includes(num)) {
    return "blue";
  } else if (config.color.green.includes(num)) {
    return "green";
  }
}
// 获取号码生肖
export function getBallZodiac(num: number) {
  const json = getZodiacJson();
  const zodiacName = { ...config.animal.beast, ...config.animal.poultry };
  for (const key in json) {
    if (json[key].includes(num)) {
      return zodiacName[key];
    }
  }
  return "鼠";
}
// 获取号码五行
export function getBallFive(num: number) {
  const json: Record<string, number[]> = config.five;
  const fiveName: Record<string, string> = {
    jin: "金",
    mu: "木",
    shui: "水",
    huo: "火",
    tu: "土"
  };
  for (const key in json) {
    if (json[key].includes(num)) {
      return fiveName[key];
    }
  }
  return "金";
}
export function getBallInfo(num: number) {
  return [String(num), getBallZodiac(num), getBallColor(num), getBallFive(num)];
}
